<?php
class Admin_CategoryController extends Zendvn_Controller_Action
{
	public function init()
	{
		$template_path=TEMPLATE_PATH."/admin/system";
		$this->loadTemplate($template_path);
	}
	public function indexAction()
	{
		
		$session=new Admin_Model_Session();
		$category = new Admin_Model_Category();
		$this->view->session=($session->getSession()->toArray());
		$tmp= $this->_request->getParam('lbparent',0);
		$this->view->category=($category->getCategory($tmp)->toArray());
		
	}
	public function addcategoryAction()
	{
		$form = new Admin_Form_Category();
		$category= new Admin_Model_Category();
		$form->submit->setLabel("add");
		
		$chk=$this->_request->getPost('hidses');
	
		$form->getElement('idSes')->setValue($chk);

		$this->view->form=$form;

		if($this->_request->isPost()&& $this->_request->getParam('submit'))
		{
			$formdata= $this->_request->getPost();
		
			if($form->isValid($formdata))
			{
				$nameCat=$form->getValue('NameCat');
				$idSes=$form->getValue('idSes');
				$keyWord=$form->getValue('keyWord');
				$category->addCategory($nameCat,$idSes,$keyWord);
				$this->view->tmp=1;
				$this->_helper->redirector('index');
				
			}
			else
			{
				$form->populate($formdata);
			}
			
		}
		
	
	}
	public function editcategoryAction()
	{
		$form = new Admin_Form_Category();
		$category= new Admin_Model_Category();
		$form->submit->setLabel("edit");
		
		$chk=$this->_request->getPost('chk');
		$tmp=explode('|',$chk[0]);
		$form->getElement('idCat')->setValue($tmp[0]);
		$form->getElement('NameCat')->setValue($tmp[1]);
		$form->getElement('idSes')->setValue($tmp[2]);
		$form->getElement('keyWord')->setValue($tmp[3]);
		
		$this->view->form=$form;
		
		if($this->_request->isPost()&& $this->_request->getParam('submit'))
		{
			$formdata= $this->_request->getPost();
			
			if($form->isValid($formdata))
			{
				$idCat=$form->getValue('idCat');
				$nameCat=$form->getValue('NameCat');
				$idSes=$form->getValue('idSes');
				$keyWord=$form->getValue('keyWord');
				$category->editCategory($idCat,$nameCat,$idSes,$keyWord);
				$this->_helper->redirector('index');
			}
			else
			{
				$form->populate($formdata);
			}
			
		}
		
	
	}
	public function deletecategoryAction()
	{
		$form = new Admin_Form_Category();
		$category= new Admin_Model_Category();
		$form->submit->setLabel("delete");
		$chk=$this->_request->getPost('chk');
		foreach($chk as $value)
		{
			$tmp[]=explode('|',$value);
	
		}
		foreach ($tmp as $id)
		{
			$category->deleteCategory($id[0]);
		}	
		
		$this->_helper->redirector('index');

		$this->_helper->viewRenderer->setNoRender();
		
		
	}
}